package cn.ac.iscas.action.project;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;


import cn.ac.iscas.bean.PageView;
import cn.ac.iscas.bean.project.Project;
import cn.ac.iscas.service.project.IProjectService;

@Controller @Scope("prototype")
public class ProjectAction {
	private ProjectQuery projectQuery;
	@Resource IProjectService projectService;
	private int page;
    
	
	
	public IProjectService getProjectService() {
		return projectService;
	}

	public void setProjectService(IProjectService projectService) {
		this.projectService = projectService;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}
	public String queryPage(){
		return "query";
	}
	public String typeIn()
	{
		return "add";
	}
	public String list()
	{
		page = (0==page)?1:page;
		PageView<Project> pageView = new PageView<Project>(7, page);
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("id", "desc");
		pageView.setQueryResult(projectService.getScrollData(Project.class,pageView.getFirstResult(),pageView.getMaxresult(), orderby));
		ActionContext.getContext().put("pageView", pageView);
		return "list";
	}
	public String query(){
		if(page<=0)page=1;
		StringBuffer jpql = new StringBuffer("");
		List<Object> params = new ArrayList<Object>();
		if(projectQuery.getProjectNo()!=null && !"".equals(projectQuery.getProjectNo())){
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.projectNo like ?");
			params.add("%"+ projectQuery.getProjectNo()+"%" );
		}
		if(projectQuery.getProjectName()!=null && !"".equals(projectQuery.getProjectName())){
			if(params.size()>0) jpql.append(" and ");
			System.out.println(projectQuery.getProjectName()+"name");
			jpql.append(" o.projectName like ?");
			params.add("%"+ projectQuery.getProjectName()+ "%");
		}
		if(projectQuery.getPersonInCharge()!=null && !"".equals(projectQuery.getPersonInCharge())){
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.personInCharge like ?");
			params.add("%"+projectQuery.getPersonInCharge()+"%");
		}
		System.out.println(projectQuery.getReposiblePerson()+"ddd");
		if(projectQuery.getReposiblePerson()!=null && !"".equals(projectQuery.getReposiblePerson())){
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.reposiblePerson like ?");
			params.add("%"+projectQuery.getReposiblePerson()+"%");
		}
		if(projectQuery.getState()!=null &&!"".equals(projectQuery.getState()))
		{
			if(params.size()>0) jpql.append(" and ");
			jpql.append(" o.state = ?");
			params.add(projectQuery.getState());
		}
		PageView<Project> pageView=new PageView<Project>(7,page);
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("id", "desc");
		pageView.setQueryResult(projectService.getScrollData(Project.class,pageView.getFirstResult(), 
				pageView.getMaxresult(), jpql.toString(), params.toArray(), orderby));
		ActionContext.getContext().put("pageView", pageView);
		return "list" ;
	}
    public String test() throws IOException
    {
    	ActionContext ctx = ActionContext.getContext();     
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		response.setContentType("text/html;charset=utf-8");
	    HttpServletRequest request = ServletActionContext.getRequest();
		System.out.println(request.getParameter("name"));
		out.println(request.getParameter("name") + ",服务器时间：" + new Date()
		    		+ ",请校对!");
		out.flush();
		out.close();
		return null;
	}
	public ProjectQuery getProjectQuery() {
		return projectQuery;
		}
    public void setProjectQuery(ProjectQuery projectQuery) {
    	this.projectQuery = projectQuery;
	}
}
